//
//  HistoryCell.m
//  NeteaseNewsLY
//
//  Created by dllo on 16/1/29.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HistoryCell.h"

@interface HistoryCell ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation HistoryCell
- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [_threePicArr release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT * 40, WIDTH * 375, HEIGHT * 667) style:UITableViewStylePlain];
        [self headerData];
        [self.contentView addSubview:_tableView];
        [_tableView release];
    }
    return self;
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    // 与首页10条信息同种cell 用NewModel/NewTableCell/ThreePicCell
    [_tableView registerClass:[NewTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [_tableView registerClass:[ThreePicCell class] forCellReuseIdentifier:@"threePiccell"];
    
    // 刷新
    MJRefreshHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self headerData];
            [self.tableView.mj_header endRefreshing];
        });
    }];
    self.tableView.mj_header = header;
    // 加载
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self footerData];
        [self.tableView.mj_footer endRefreshing];
    }];
    self.tableView.mj_footer = footer;
}
// 刷新
- (void)headerData
{
    NSString *str = @"http://api.iclient.ifeng.com/ClientNews?id=LS153,FOCUSLS153&page=1&gv=4.4.6&av=4.4.6&uid=359786057270002&deviceid=359786057270002&proid=ifengnews&os=android_21&df=androidphone&vt=5&screen=1080x1920&publishid=2009";
    [AFHTTPHandler GETWithURL:str completion:^(id result) {
        NSArray *arr = result[0][@"item"];
        self.dataArr = [NewModel modelHandleWithArray:arr];
        self.threePicArr = result[0][@"slides"];
        //        self.SecondDataArr = [NewModel modelHandleWithArray:arr2];
        // 遍历第二种cell数据结果 将value值放入同一个数组
        for (NSDictionary *dic in _threePicArr) {
            NewModel *m = [[NewModel alloc] init];
            [m setValuesForKeysWithDictionary:dic];
            [self.dataArr addObject:m];
            [m release];
        }
        // 打乱同数组中两种cell数据的顺序
        for (int i = 0; i < _dataArr.count; i++) {
            int m = arc4random() %_dataArr.count;
            [_dataArr exchangeObjectAtIndex:i withObjectAtIndex:m];
        }
        
        [self.tableView reloadData];
    }];
    
}
// 加载
- (void)footerData
{
    static NSInteger t = 2;
    NSString *str = @"http://api.iclient.ifeng.com/ClientNews?id=LS153,FOCUSLS153&page=1&gv=4.4.6&av=4.4.6&uid=359786057270002&deviceid=359786057270002&proid=ifengnews&os=android_21&df=androidphone&vt=5&screen=1080x1920&publishid=2009";
    NSString *urlStr = [str stringByReplacingOccurrencesOfString:@"page=1" withString:[NSString stringWithFormat:@"page=%ld", t]];
    [AFHTTPHandler GETWithURL:urlStr completion:^(id result) {
        NSArray *arr = result[0][@"item"];
        self.dataArr = [NewModel modelHandleWithArray:arr];
        self.threePicArr = result[0][@"slides"];
        //        self.SecondDataArr = [NewModel modelHandleWithArray:arr2];
        // 遍历第二种cell数据结果 将value值放入同一个数组
        for (NSDictionary *dic in _threePicArr) {
            NewModel *m = [[NewModel alloc] init];
            [m setValuesForKeysWithDictionary:dic];
            [self.dataArr addObject:m];
            [m release];
        }
        // 打乱同数组中两种cell数据的顺序
        for (int i = 0; i < _dataArr.count; i++) {
            int m = arc4random() %_dataArr.count;
            [_dataArr exchangeObjectAtIndex:i withObjectAtIndex:m];
        }
        
        [self.tableView reloadData];
    }];
    t += 1;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewModel *m = _dataArr[indexPath.row];
    if ([m.type isEqualToString:@"slide"]) {
        ThreePicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"threePiccell" forIndexPath:indexPath] ;
        [cell.oneImg sd_setImageWithURL:[NSURL URLWithString:m.images[0]] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
        [cell.twoImg sd_setImageWithURL:[NSURL URLWithString:m.images[1]] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
        [cell.threeImg sd_setImageWithURL:[NSURL URLWithString:m.images[2]] placeholderImage:[UIImage imageNamed:@"zhanwie"]];
        cell.title.text = m.title;
        cell.updateTime.text = m.updateTime;
        cell.updateTime.text = [m.updateTime substringWithRange:NSMakeRange(11, 5)];
        cell.commentsall.text = m.commentsall;
        return cell;
    }
    else{
        NewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]  ;
        [cell.picture sd_setImageWithURL:[NSURL URLWithString:m.thumbnail] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
        cell.title.text = m.title ;
        cell.updateTime.text = [m.updateTime substringWithRange:NSMakeRange(11, 5)];
        cell.commentsall.text = [m.commentsall stringByAppendingString:@"跟帖"];
        return cell;
        
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewModel *m = _dataArr[indexPath.row];
    if ([m.type isEqualToString:@"slide"]) {
        return HEIGHT * 150;
    }else{
        return HEIGHT * 110;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewModel *m = _dataArr[indexPath.row];
    //    HistorySelectVC *hisSelect = [[HistorySelectVC alloc] init];
    //    hisSelect.str = m.commentsUrl;
    //    [self.navigationController pushViewController:hisSelect animated:YES];
    //    [hisSelect release];
    [self.delegate historyValue:m.commentsUrl];
}

@end
